#include "modbusinstructiontest.h"
#include "ui_modbusinstructiontest.h"

modbusInstructionTest::modbusInstructionTest(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::modbusInstructionTest)
{
    ui->setupUi(this);
    programSet = new ProgramSet;
    programTextTransfer = new ProgramTextTransfer(0);
    cmdStrList_out.clear();
    pointList_out.clear();
    regPlList_out.clear();
    palletStackInfo_out.clear();

    QString filePath = "/tcrwork/linuxController/trunk/TcrController/data/robotData1/program/modbusConfig.txt";

//    int resultKey = programEngine->readOneProgramFile(filePath);
//    ui->Button_ModbusStart->setText(QString("%1").arg(resultKey));
    int transferTextKey = programTextTransfer->transferText(filePath,cmdStrList_out,pointList_out,regPlList_out,palletStackInfo_out);
    qDebug() << "modbusInstructionTest::transferText === transferTextKey:" << transferTextKey;
    int resultKey = programSet->transferTextInstructionToStructData(cmdStrList_out, 0,"ModbusInstruction",0,0);
    qDebug() << "modbusInstructionTest::transferTextInstructionToStructData ==== resultKey:" << resultKey;

}

modbusInstructionTest::~modbusInstructionTest()
{
    delete ui;
}

void modbusInstructionTest::on_Button_ModbusStart_clicked()
{
        if(programSet->first().data()->modbusStartInstruction.instructionType== MODBUS_START)
        {
            ui->Edit_ModbusStart_Type->setText("MODBUS_STAR");
            ui->Edit_ModbusStart_BusId->setText(QString("%1").arg(programSet->first().first().modbusStartInstruction.busId));
            if(programSet->first().first().modbusStartInstruction.modbusType == E_MODBUS_TYPE_INTERNET_MASTER)
            {
                ui->Edit_ModbusStart_BusType->setText("INTERNET");
                ui->Edit_ModbusStart_Mode->setText("MASTER");
            }
            else if( programSet->first().first().modbusStartInstruction.modbusType == E_MODBUS_TYPE_INTERNET_SLAVE )
            {
                ui->Edit_ModbusStart_BusType->setText("INTERNET");
                ui->Edit_ModbusStart_Mode->setText("SLAVE");
            }
            else if( programSet->first().first().modbusStartInstruction.modbusType == E_MODBUS_TYPE_RS485_MASTER )
            {
                ui->Edit_ModbusStart_BusType->setText("RS485");
                ui->Edit_ModbusStart_Mode->setText("MASTER");
            }
            else if( programSet->first().first().modbusStartInstruction.modbusType == E_MODBUS_TYPE_RS485_SLAVE )
            {
                ui->Edit_ModbusStart_BusType->setText("RS485");
                ui->Edit_ModbusStart_Mode->setText("SLAVE");
            }
            else
            {
                ui->Edit_ModbusStart_BusType->setText("ERROR");
            }

            ui->Edit_ModbusStart_BusId->setText(QString("%1").arg(programSet->first().first().modbusStartInstruction.busId));
            ui->Edit_ModbusStart_Label->setText(programSet->first().first().modbusStartInstruction.jmpLable);

        }
        else
        {
            ui->Edit_ModbusStart_Type->setText("ERROR");
        }
}

void modbusInstructionTest::on_Button_ModbusRead_clicked()
{
    if(programSet->first().value(2).modbusReadInstruction.instructionType== MODBUS_READ)
    {
        ui->Edit_ModbusRead_Type->setText("MODBUS_READ");
        ui->Edit_ModbusRead_BusId->setText(QString("%1").arg(programSet->first().value(2).modbusReadInstruction.busId));
        if(programSet->first().value(2).modbusReadInstruction.modbusType == E_MODBUS_TYPE_INTERNET_MASTER)
        {
            ui->Edit_ModbusRead_BusType->setText("INTERNET");
            ui->Edit_ModbusRead_Mode->setText("MASTER");
        }
        else if( programSet->first().value(2).modbusReadInstruction.modbusType == E_MODBUS_TYPE_INTERNET_SLAVE )
        {
            ui->Edit_ModbusRead_BusType->setText("INTERNET");
            ui->Edit_ModbusRead_Mode->setText("SLAVE");
        }
        else if( programSet->first().value(2).modbusReadInstruction.modbusType == E_MODBUS_TYPE_RS485_MASTER )
        {
            ui->Edit_ModbusRead_BusType->setText("RS485");
            ui->Edit_ModbusRead_Mode->setText("MASTER");
        }
        else if( programSet->first().value(2).modbusReadInstruction.modbusType == E_MODBUS_TYPE_RS485_SLAVE )
        {
            ui->Edit_ModbusRead_BusType->setText("RS485");
            ui->Edit_ModbusRead_Mode->setText("SLAVE");
        }
        else
        {
            ui->Edit_ModbusRead_BusType->setText("ERROR");
        }

        ui->Edit_ModbusRead_BusId->setText(QString("%1").arg(programSet->first().value(2).modbusReadInstruction.busId));
        ui->Edit_ModbusRead_Label->setText(programSet->first().value(2).modbusReadInstruction.jmpLable);
        ui->Edit_Read_RegId->setText(QString("%1").arg(programSet->first().value(2).modbusReadInstruction.modbuReadRegAddress));
        ui->Edit_Read_Value->setText(QString("%1").arg(programSet->first().value(2).modbusReadInstruction.modbusReadDataRegisterIndex));

    }
    else
    {
        ui->Edit_ModbusRead_Type->setText("ERROR");
    }
}

void modbusInstructionTest::on_Button_ModbusWrite_clicked()
{
    if(programSet->first().value(4).modbusWriteInstruction.instructionType== MODBUS_WRITE)
    {
        ui->Edit_ModbusWrite_Type->setText("MODBUS_WRITE");
        ui->Edit_ModbusWrite_BusId->setText(QString("%1").arg(programSet->first().value(4).modbusWriteInstruction.busId));
        if(programSet->first().value(4).modbusWriteInstruction.modbusType == E_MODBUS_TYPE_INTERNET_MASTER)
        {
            ui->Edit_ModbusWrite_BusType->setText("INTERNET");
            ui->Edit_ModbusWrite_Mode->setText("MASTER");
        }
        else if(programSet->first().value(4).modbusWriteInstruction.modbusType == E_MODBUS_TYPE_INTERNET_SLAVE )
        {
            ui->Edit_ModbusWrite_BusType->setText("INTERNET");
            ui->Edit_ModbusWrite_Mode->setText("SLAVE");
        }
        else if(programSet->first().value(4).modbusWriteInstruction.modbusType == E_MODBUS_TYPE_RS485_MASTER )
        {
            ui->Edit_ModbusWrite_BusType->setText("RS485");
            ui->Edit_ModbusWrite_Mode->setText("MASTER");
        }
        else if( programSet->first().value(4).modbusWriteInstruction.modbusType == E_MODBUS_TYPE_RS485_SLAVE )
        {
            ui->Edit_ModbusWrite_BusType->setText("RS485");
            ui->Edit_ModbusWrite_Mode->setText("SLAVE");
        }
        else
        {
            ui->Edit_ModbusWrite_BusType->setText("ERROR");
        }

        ui->Edit_ModbusWrite_BusId->setText(QString("%1").arg(programSet->first().value(4).modbusWriteInstruction.busId));
        ui->Edit_ModbusWrite_Label->setText(programSet->first().value(4).modbusWriteInstruction.jmpLable);
        ui->Edit_Write_RegId->setText(QString("%1").arg(programSet->first().value(4).modbusWriteInstruction.modbuWriteRegAddress));
        ui->Edit_Write_Value->setText(QString("%1").arg(programSet->first().value(4).modbusWriteInstruction.modbusWriteValueRegisterAddress));

    }
    else
    {
        ui->Edit_ModbusWrite_Type->setText("ERROR");
    }
}
